package com.lu.library.ext

import android.os.Bundle
import android.os.Parcelable
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.launcher.ARouter
import kotlinx.serialization.Serializable
import kotlin.reflect.KClass
import java.lang.reflect.ParameterizedType
fun String.navigation():Any?{
   return ARouter.getInstance().build(this).navigation()
}
fun String.navigation(key:String,value:Int):Any?{
   return ARouter.getInstance().build(this)
      .withInt(key, value)
      .navigation()
}


inline fun <reified T> String.navigation(key: String, value: T?): Any? {
   val postcard = ARouter.getInstance().build(this)
   when (value) {
      null -> postcard.withString(key, null)
      is String -> postcard.withString(key, value)
      is Int -> postcard.withInt(key, value)
      is Float -> postcard.withFloat(key, value)
      is Boolean -> postcard.withBoolean(key, value)
      is Byte -> postcard.withByte(key, value)
      is ByteArray -> postcard.withByteArray(key, value)
      is Char -> postcard.withChar(key, value)
      is CharArray -> postcard.withCharArray(key, value)
      is Double -> postcard.withDouble(key, value)
      is Long -> postcard.withLong(key, value)
      is FloatArray -> postcard.withFloatArray(key, value)
      is Parcelable -> postcard.withParcelable(key, value)
      is Short -> postcard.withShort(key, value)
      is ShortArray -> postcard.withShortArray(key, value)
      is ArrayList<*> -> {
         val type = (T::class.java.genericSuperclass as? ParameterizedType)
         val actualTypeArguments = type?.actualTypeArguments
         if (!actualTypeArguments.isNullOrEmpty()) {
            val itemType = actualTypeArguments[0]
            when (itemType) {
               String::class.java -> postcard.withStringArrayList(key, value as ArrayList<String>)
               Parcelable::class.java -> postcard.withParcelableArrayList(key, value as ArrayList<Parcelable>)
               Int::class.java -> postcard.withIntegerArrayList(key, value as ArrayList<Int>)
               CharSequence::class.java -> postcard.withCharSequenceArrayList(key, value as ArrayList<CharSequence>)
               else -> throw IllegalArgumentException("Unsupported ArrayList element type: ${itemType}")
            }
         } else {
            throw IllegalArgumentException("Unable to determine ArrayList element type")
         }
      }
      is java.io.Serializable -> postcard.withSerializable(key, value)
   }
   return postcard.navigation() // 返回期望的结果
}

fun String.build(): Postcard{
   return ARouter.getInstance().build(this)
}